package myhibernate;

import java.util.List;

import mytools.Tool;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * TRepair entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see myhibernate.TRepair
 * @author MyEclipse Persistence Tools
 */

public class TRepairDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TRepairDAO.class);
	// property constants
	public static final String REPAIR_DETAILS = "repairDetails";

	public void save(TRepair transientInstance) {
		log.debug("saving TResident instance");
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			if (tx != null) {
				tx.rollback();
			}
			log.error("save failed", re);
			re.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void delete(TRepair transientInstance) {
		log.debug("deleting TResident instance");
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(transientInstance);
			tx.commit();
			log.debug("delete successful");
			Tool.successStatus();
		} catch (RuntimeException re) {
			if (tx != null) {
				tx.rollback();
			}
			log.error("delete failed", re);
			// re.printStackTrace();
			Tool.failStatus();
		} finally {
			session.close();
		}
	}

	public void update(TRepair transientInstance) {
		log.debug("updating TResident instance");
		Session session = getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(transientInstance);
			tx.commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			if (tx != null) {
				tx.rollback();
			}
			log.error("update failed", re);
			re.printStackTrace();
		} finally {
			session.close();
		}
	}

	public TRepair findById(java.lang.Integer id) {
		log.debug("getting TRepair instance with id: " + id);
		Session session = getSession();
		try {
			TRepair instance = (TRepair) session.get("myhibernate.TRepair", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	public List findByExample(TRepair instance) {
		log.debug("finding TRepair instance by example");
		try {
			List results = getSession().createCriteria("myhibernate.TRepair")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TRepair instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TRepair as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByRepairDetails(Object repairDetails) {
		return findByProperty(REPAIR_DETAILS, repairDetails);
	}

	public List findAll() {
		log.debug("finding all TRepair instances");
		Session session = getSession();
		try {
			String queryString = "from TRepair";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	public TRepair merge(TRepair detachedInstance) {
		log.debug("merging TRepair instance");
		try {
			TRepair result = (TRepair) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TRepair instance) {
		log.debug("attaching dirty TRepair instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TRepair instance) {
		log.debug("attaching clean TRepair instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}